﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Data;
using Matrix.Common.Utility;
using Matrix.MaKaYunShang.Model;
using Matrix.MaKaYunShang.DAL;
using Matrix.MaKaYunShang.Model.Dto;

namespace Matrix.MaKaYunShang.BLL
{
    public partial class UserCardBLL : BaseBLL<UserCardBLL>
    {
        #region 默认方法
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(UserCard model)
        {
            return UserCardDAL.Instance.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(UserCard model)
        {
            return UserCardDAL.Instance.Update(model);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            return UserCardDAL.Instance.Delete(Id);
        }
        /// <summary>
        /// 批量删除一批数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            return UserCardDAL.Instance.DeleteList(Idlist);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public UserCard GetModel(int Id)
        {
            var ds = UserCardDAL.Instance.GetModel(Id);
            return ds == null ? null : ServicesHelper.DataSetToIList<UserCard>(ds).ToList()[0];
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<UserCard> GetModelList(string strWhere = null)
        {
            var ds = UserCardDAL.Instance.GetList(strWhere);
            return ServicesHelper.DataSetToIList<UserCard>(ds).ToList();
        }
        #endregion

        #region 扩展方法
        /// <summary>
        /// 用户卡卷列表
        /// </summary>
        /// <param name="userId">领用人用户id</param>
        /// <param name="cardStatus">卡卷状态</param>
        /// <remarks>create 2016-06-
        public JsonModel.PageList<UserCard> GetUserCardList(int pageIndex, int pageSize, int userId, int cardStatus)
        {
            try
            {
                var totalCount = 0;
                var list = UserCardDAL.Instance.GetUserCardList(pageIndex, pageSize, userId, cardStatus, ref totalCount);
                return new JsonModel.PageList<UserCard>(list, totalCount);
            }
            catch (Exception ex)
            {
                _fileLog.Error(" GetList | Exception: " + ex);
                return null;
            }
        }

        /// <summary>
        /// 根据类别查询用户 卡卷记录
        /// </summary>
        /// <returns></returns>
        /// <remarks>create lk 2016-06-12</remarks>
        public JsonModel.PageList<UserCard> GetUserCardByCategoryCodeList(int pageIndex, int pageSize, int userId, string categoryCodeStr, string keyWord, int cardStatus)
        {
            try
            {
                var totalCount = 0;
                var list = UserCardDAL.Instance.GetUserCardByCategoryCodeList(pageIndex, pageSize, userId, ref totalCount, categoryCodeStr, keyWord, cardStatus);
                return new JsonModel.PageList<UserCard>(list, totalCount);
            }
            catch (Exception ex)
            {
                _fileLog.Error(" GetList | Exception: " + ex);
                return null;
            }
        }
        /// <summary>
        /// 查询卡劵详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <remarks>create lk 2016-06-12</remarks>

        public UserCard GetModelByUserCardId(int Id)
        {
            DataSet ds = UserCardDAL.Instance.GetModelByUserCardId(Id);
            return ds == null ? null : ServicesHelper.DataSetToIList<UserCard>(ds).ToList()[0];
        }

        public List<UserCard> GetModelByStrWhere(string strWhere = null)
        {
            DataSet ds = UserCardDAL.Instance.GetModelByStrWhere(strWhere);
            return ServicesHelper.DataSetToIList<UserCard>(ds).ToList();
        }
        public UserCard GetModelByCardCode(string cardCode)
        {
            DataSet ds = UserCardDAL.Instance.GetModelByCardCode(cardCode);
            return ds == null ? null : ServicesHelper.DataSetToIList<UserCard>(ds).ToList()[0];
        }
        /// <summary>
        /// 查询卡劵id每天的扫码次数
        /// </summary>
        /// <param name="cloudCardId"></param>
        /// <remarks>create lk 2016-06-13</remarks>

        public int GetModelByCloudCardIdCount(int cloudCardId)
        {

            return UserCardDAL.Instance.GetModelByCloudCardIdCount(cloudCardId);

        }

        public List<UserCardTimeGroupDto> GetVerifyDataList(int userId)
        {
            DataSet ds = UserCardDAL.Instance.GetVerifyDataList(userId);
            return ServicesHelper.DataSetToIList<UserCardTimeGroupDto>(ds).ToList();
        }
        public List<UserCard> GetMdelByCategoryCode(string categoryCode, int cardStatus)
        {
            DataSet ds = UserCardDAL.Instance.GetMdelByCategoryCode(categoryCode, cardStatus);
            return ServicesHelper.DataSetToIList<UserCard>(ds).ToList();
        }

        public int GetUsableQrCodeCount(int receiveUserId)
        {
            return UserCardDAL.Instance.GetUsableQrCodeCount(receiveUserId);
        }

        /// <summary>
        /// 获得分页数据列表
        /// </summary>
        public TableDataViewDto<ViewUserCardDto> GetPagingList(int draw, int pageIndex, int pageSize, string searchKeyWord, string colOrder, int cardStatus, string shopIds, bool isOnlySearchVerifyShop, string startTime, string endTime, int cateId)
        {
            try
            {
                var totalCount = 0;
                var list = UserCardDAL.Instance.GetPagingList(pageIndex, pageSize, searchKeyWord, colOrder, cardStatus, shopIds, isOnlySearchVerifyShop, startTime, endTime, cateId, ref totalCount);
                var returnDataList = new TableDataViewDto<ViewUserCardDto>
                {
                    data = list,
                    draw = draw,
                    recordsFiltered = totalCount,
                    recordsTotal = totalCount
                };
                return returnDataList;
            }
            catch (Exception ex)
            {
                _fileLog.Error(" GetPagingList | Exception: " + ex);
                return null;
            }
        }

        public TableDataViewDto<ViewUserCardDto> GetPagingList(int draw, int pageIndex, int pageSize, string searchKeyWord, string colOrder, string categoryCodeStr, string shopIds, int cardStatus)
        {
            try
            {
                var totalCount = 0;
                var list = UserCardDAL.Instance.GetPagingList(pageIndex, pageSize, searchKeyWord, colOrder, categoryCodeStr, shopIds, cardStatus, ref totalCount);
                list.ForEach(s =>
                {
                    if (string.IsNullOrEmpty(s.LogisticsNum) || string.IsNullOrEmpty(s.LogisticsCompany))
                    { 
                    s.Action = "<a class='btn btn-warning' data-target=\"#LogisticsModal\" data-toggle=\"modal\" href='javascript:;' onclick=AddLogistics(" + s.Id +
                                  ");>设置为已邮寄</a> ";
                    }
                }
            );
                var returnDataList = new TableDataViewDto<ViewUserCardDto>
                {
                    data = list,
                    draw = draw,
                    recordsFiltered = totalCount,
                    recordsTotal = totalCount
                };
                return returnDataList;
            }
            catch (Exception ex)
            {
                _fileLog.Error(" GetPagingList | Exception: " + ex);
                return null;
            }
        }
        #endregion
    }
}